define(["../common"], function() {
	// 开关相关
	$(function() {
		// 初始化选中状态
		var memPromote = $("#memPromote").val();
		var savePath = g_firmDomain + "/company/memPromoteDo/json";
	    $("[name='types']").each(function(i, el) {
	    	if ((memPromote & (1 << $(el).val())) > 0) {
	    		$(el).prop("checked", true);
	    	}
	    });
	    
	    // 事件:保存开关状态
	    $("#typeSaveBt").click(function() {
	    	submitDo_();
	    });
	    
	    function submitDo_() {
	    	if (!$(document).showMsg("正在保存...", 'load', true)) {
	    		return;
	    	}
	    	memPromote = 0;
	    	$("[name='types']:checked").each(function(i, el) {
	        	memPromote = memPromote | (1 << $(el).val());
	        });
	        
	    	$.post(savePath, {memPromote: memPromote}, function(data) {
	    		if (data.status !== 0) {
	    			$(document).showMsg(data.message, 'error');
	    			return;
	    		}
	    		$(document).showMsg("保存成功", 'pass');
	    	});
	    }
	});

	$(function() {
		var curType = 0;
		var loaded = {
			"0": false,
			"1": false,
			"2": false
		};
		var $tip = $("#tip");
	    var temp = {
			labels: {
				"0": "每次充值满",
				"1": "累计充值满",
				"2": "最高充值满"
			},
			ctrls: {
				"0": "送",
				"1": "送",
				"2": "打"
			},
			units: {
				"0": "元",
				"1": "元",
				"2": "折"
			},
			html: $("#temp").html()
		};
	    // 表单验证设置
	    var formConfig = {
	    	rules: {
	    		test: function(element, params) {
	    			return (
	    					this.test(element, "gt[0]") === true && 
	    					this.test(element, "money") === true && 
	    					this.test(element, "range[~999999]") === true
	    					) || '{0}无效值';
	    		}
	    	},
	    	fields: {
	    		prepaid: 'required;test',
	    		give: 'required;test'
	    	}
	    };
	    
		
		// 开始加载
	    load_(curType);
	    
	    loadjs(["validator"], function() {
	    	$("form").validator(formConfig);
	    });
		
		// 切换类型
		$(".tab_menu li").click(function() {
			$(".tab_menu li").removeClass("selected");
			$(this).addClass("selected");
			$tip.hide();
			
			curType = ($(this).attr("id").split("_"))[1];
			
			// 未加载过的加载
			if (!loaded[curType]) {
				load_(curType);
			}	
			// 显示相应表单
			$("form").hide();
			var $form = $("#form_" + curType).fadeIn();
			if ($form.its("list").is(":empty")) {
				$tip.show().html("查无记录");
			}
				
		});
			
		// 事件:增加项
	    $("[its='addBt']").click(function() {
	    	$tip.hide();
	    	appendItem_($("#form_" + curType).its("list"));
	    });
	    // 事件:提交验证
	    $("[its='submitBt']").click(function() {
	    	$(this).parents("form").trigger('validate');
	    });
	    // 事件:重置
	    $("[its='resetBt']").click(function() {
	    	load_(curType);
	    });
	    // 事件:删除项
	    $(document).on('click', "[its='delBt']", function() {
	    	$(this).parent("p").fadeOut(function() {$(this).remove();});
	    });
	    
	    // 事件:提交表单
	    $("form").on('valid.form', function(e, form) {
	    	if (!$(form).showMsg("正在保存...", 'load', true)) {
	    		return;
	    	}
	    	// 复制表单对象并重命名表单项
	    	$form = $(form).clone();
	    	$form.find("[name='prepaid']").each(function(i, el) {
	    		$(this).attr("name", "mps[" + i + "].prepaid");
	    	});
	    	$form.find("[name='give']").each(function(i, el) {
	    		$(this).attr("name", "mps[" + i + "].give");
	    	});
	    	// 提交表单副本
	    	$.post(g_firmDomain + "/memberPromote/addDo/json", $form.serialize(), function(data) {
	    		if (data.status !== 0) {
	    			$(form).showMsg(data.message, 'error');
	    			return;
	    		}
	    		$(form).showMsg("保存成功!", 'pass');
	    	});
	    });
	    
	    function load_(type) {
	    	var $form = $("#form_" + type);
	    	var $list = $form.its("list");
	    	$list.empty();
	        $form.hide();
	    	$tip.show().html("正在加载...");
	    	$.get(g_firmDomain + "/memberPromote/list/json?r=" + Math.random(), {type: type}, function(data) {
	        	if (data.status !== 0) {
	        		$(document).showMsg(data.message, 'error');
	        		return;
	        	}
	        	loaded[curType] = true;
	        	$form.show();
	        	var list = data.result;
	        	if (!list) {
	        		$tip.html("查无记录");
	        		return;
	        	}
	        	$tip.hide();
	        	for (var i in list) {
	        		appendItem_($list, list[i]);// list[i] -> memberPromote
	        	}
	        	
	        });
	    }
	    
	    function appendItem_($list, data) {
	    	if (!data) {
	    		data = {
	    			prepaid: "",
	    			give: ""
	    		};
	    	}
	    	var html = temp.html
	  		.replace(/\-\{r\}\-/g, parseInt(Math.random() * 100000))
	  		.replace(/\-\{ctrl\}\-/g, temp.ctrls[curType])
	  		.replace(/\-\{label\}\-/g, temp.labels[curType])
	  		.replace(/\-\{unit\}\-/g, temp.units[curType])
	  		.replace(/\-\{prepaid\}\-/g, data.prepaid?data.prepaid : 0)// (ie兼容)如果是空值,生成的文本框内容为"type=text"
	  		.replace(/\-\{give\}\-/g, data.give?data.give : 0); // (ie兼容)同上
	  		var $item = $('<div>').html(html).children();
	  		$item.appendTo($list);
	        $item.fadeIn();
	    }
	});

});
